package dat255.calendarclock.model.event;

import java.util.GregorianCalendar;
import java.util.Observable;
import java.util.Timer;
import java.util.TimerTask;

import android.util.Log;

public class MinuteTimer extends Observable {

	// Field

	private static MinuteTimer singleInstance;
	
	
	public void run(long delay){

		int period = 60000; // repeat every sec.

		Timer timer = new Timer();

		timer.scheduleAtFixedRate(new TimerTask() {

			public void run() {
				Log.d("debug", "In run");
				getInstance().setChanged();
				getInstance().notifyObservers();
			}

		}, delay, period);

	}

	// Constructor

	private MinuteTimer() {
		GregorianCalendar calendar = new GregorianCalendar();
		long currentTimestamp = calendar.getTimeInMillis();
		calendar = DateHelper.reset(calendar, GregorianCalendar.MINUTE);
		calendar.add(GregorianCalendar.MINUTE, 1);
		
		long countdown = calendar.getTimeInMillis() - currentTimestamp;
		run(countdown);

	}

	public static MinuteTimer getInstance() {
		if (singleInstance == null)
			singleInstance = new MinuteTimer();
		return singleInstance;
	}

}